package managedbean;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.ejb.EJB;
import java.sql.Timestamp;

import javax.faces.model.SelectItem;

import entity.Log;
import java.util.List;
import javax.faces.bean.ManagedProperty;
import session.stateless.LogSessionBeanLocal;

/**
 *
 * @author JASMINE
 */
@ManagedBean(name = "logManagerBean")
@RequestScoped
public class LogManagerBean implements Serializable {

    @ManagedProperty(value = "#{loginManagerBean}")
    private LoginManagerBean loginManagerBean;
    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    @EJB
    private LogSessionBeanLocal logSessionBeanLocal;
    private Long logId;
    private String logType;
    private String logContent;
    private Timestamp logDate;
    private Log log;
    private List<Log> logs;
    private SelectItem[] logTypeOptions;
    private String[] logTypes;
    private String loadLogList;

    public LogManagerBean() {
        
    }

    public void loadLogs() {
        setLogs(logSessionBeanLocal.getLogs());
    }

    public void loadLogType() { //load logs from database
        List<String> s = logSessionBeanLocal.getLogTypes();
        int i = 0;
        setLogTypes(new String[s.size()]);

        for (String str : s) {
            getLogTypes()[i] = str;
            i++;
        }

        logTypeOptions = createLogTypeFilterOptions(logTypes); //populate filtering option for logType

    }

    private SelectItem[] createLogTypeFilterOptions(String[] logTypes) {
        SelectItem[] options = new SelectItem[logTypes.length + 1];

        options[0] = new SelectItem("", "Select");
        for (int i = 0; i < logTypes.length; i++) {
            options[i + 1] = new SelectItem(logTypes[i], logTypes[i]);
        }

        return options;
    }

    // <editor-fold defaultstate="collapsed" desc="Get and Set Methods">
    /**
     * @return the loginManagerBean
     */
    public LoginManagerBean getLoginManagerBean() {
        return loginManagerBean;
    }

    /**
     * @param loginManagerBean the loginManagerBean to set
     */
    public void setLoginManagerBean(LoginManagerBean loginManagerBean) {
        this.loginManagerBean = loginManagerBean;
    }

    /**
     * @return the userSessionManagerBean
     */
    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    /**
     * @param userSessionManagerBean the userSessionManagerBean to set
     */
    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    /**
     * @return the logSessionBeanLocal
     */
    public LogSessionBeanLocal getLogSessionBeanLocal() {
        return logSessionBeanLocal;
    }

    /**
     * @param logSessionBeanLocal the logSessionBeanLocal to set
     */
    public void setLogSessionBeanLocal(LogSessionBeanLocal logSessionBeanLocal) {
        this.logSessionBeanLocal = logSessionBeanLocal;
    }

    /**
     * @return the logs
     */
    public List<Log> getLogs() {

        return logs;
    }

    /**
     * @param logs the logs to set
     */
    public void setLogs(List<Log> logs) {
        this.logs = logs;
    }

    /**
     * @return the logId
     */
    public Long getLogId() {
        return logId;
    }

    /**
     * @param logId the logId to set
     */
    public void setLogId(Long logId) {
        this.logId = logId;
    }

    /**
     * @return the logType
     */
    public String getLogType() {
        return logType;
    }

    /**
     * @param logType the logType to set
     */
    public void setLogType(String logType) {
        this.logType = logType;
    }

    /**
     * @return the logContent
     */
    public String getLogContent() {
        return logContent;
    }

    /**
     * @param logContent the logContent to set
     */
    public void setLogContent(String logContent) {
        this.logContent = logContent;
    }

    /**
     * @return the logDate
     */
    public Timestamp getLogDate() {
        return logDate;
    }

    /**
     * @param logDate the logDate to set
     */
    public void setLogDate(Timestamp logDate) {
        this.logDate = logDate;
    }

    /**
     * @return the log
     */
    public Log getLog() {
        return log;
    }

    /**
     * @param log the log to set
     */
    public void setLog(Log log) {
        this.log = log;
    }

    /**
     * @return the logTypeOptions
     */
    public SelectItem[] getLogTypeOptions() {
        return logTypeOptions;
    }

    /**
     * @param logTypeOptions the logTypeOptions to set
     */
    public void setLogTypeOptions(SelectItem[] logTypeOptions) {
        this.logTypeOptions = logTypeOptions;
    }

    /**
     * @return the logTypes
     */
    public String[] getLogTypes() {
        return logTypes;
    }

    /**
     * @param logTypes the logTypes to set
     */
    public void setLogTypes(String[] logTypes) {
        this.logTypes = logTypes;
    }

    /**
     * @return the loadLogList
     */
    public String getLoadLogList() {
        loadLogs();
        loadLogType();
        return null;
    }

    /**
     * @param loadLogList the loadLogList to set
     */
    public void setLoadLogList(String loadLogList) {
        this.loadLogList = loadLogList;
    }
    // </editor-fold>
}
